package com.atguigu.mr.order;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @description: xxx
 * @time: 20191228 21:46
 * @author: baojinlong
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderBean implements WritableComparable<OrderBean> {
    /**
     * 编号
     */
    private int orderId;
    /**
     * 价格
     */
    private double price;

    @Override
    public int compareTo(OrderBean orderBean) {
        // 需要进行二次排序按照id升序,价格的降序 1是正序
        int result = 0;
        if (orderId > orderBean.getOrderId()) {
            result = 1;
        } else if (orderId < orderBean.getOrderId()) {
            result = -1;
        } else {
            if (price > orderBean.getPrice()) {
                result = -1;
            } else if (price < orderBean.getPrice()) {
                result = 1;
            }
        }
        return result;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(orderId);
        out.writeDouble(price);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        orderId = in.readInt();
        price = in.readDouble();
    }

    @Override
    public String toString() {
        return "OrderBean{" +
                "orderId=" + orderId +
                ", price=" + price +
                '}';
    }
}
